package com.mo.ld.activity.animation

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.mo.ld.R
import com.mo.ld.databinding.ActivityPokerBinding
import com.mo.lib.ui.activity.BaseVbActivity
import com.xuexiang.xutil.display.ScreenUtils
import java.util.*

/**
 * @ author：mo
 * @ data：2022/3/28:10:01
 * @ 功能：
 */
class PokerActivity : BaseVbActivity<ActivityPokerBinding>() {
    var tag = "data"

    /** 整个屏幕的宽  */
    private var width = 0

    /** 整个屏幕的高  */
    private var height = 0

    /** 上边距  */
    private var padingtop = 20

    /** 下边距  */
    private var padingdown = 20

    /** 左边距  */
    private var padingleft = 20

    /** 右边距  */
    private var padingright = 20

    /** 图片宽度  */
    private var imagewidth = 0

    /** 图片高度  */
    private var imageheight = 0

    /** 图片重叠宽度  */
    private val overlapwidh = 15

    /** 图片重叠高度  */
    private val overlapheight = 15

    /** 图片集合  */
    private val list: ArrayList<ImageView> = ArrayList()

    /** 行、列比例  */
    private val mScale = 5

    /** 创建位置个数  */
    private val POSITION = mScale * mScale

    /** 显示个数  */
    private val DISPLAY = POSITION
    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        //获取屏幕的宽高
        width = ScreenUtils.getScreenWidth()
        height = ScreenUtils.getScreenHeight()

        //上下左右页边距设为整个屏幕宽高的百分之五（如果屏幕高为100除以20为5）
        padingtop = height / 20
        padingdown = height / 20
        padingleft = width / 20
        padingright = width / 20

        //图片的宽高
        imagewidth = (width - padingleft - padingright - overlapwidh * mScale) / mScale
        imageheight = (height - padingdown - padingtop * 3 - overlapheight * mScale) / mScale

        //数值数据准备完毕
        action()
        viewBinding.tvPokerReturn.setOnClickListener(View.OnClickListener { action() })
    }
    private fun action() {
       viewBinding.rlPoker.removeAllViews()
        viewBinding.tvPokerReturn.setVisibility(View.GONE)
        object : Thread() { //开线程
            override fun run() { //清空list集合
                list.clear() //创建78张图片
                try {
                    for (i in 1..DISPLAY) { //以10毫秒一张的速度创建
                        sleep(100) //创建ImageView
                        val imageView = ImageView(this@PokerActivity) //添加ImageView
                        list.add(imageView)

                        val msg = Message()  //创建消息
                        msg.obj = imageView //添加东西
                        msg.what = i//添加数量
                        handlerShow.post { handlerShow.sendMessage(msg) }
                    }
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }
            }
        }.start()
        viewBinding.rlPoker.setEnabled(true)
    }

    /**
     * 78张图片旋转飞出布满屏幕线程
     */
val handlerShow: Handler = @SuppressLint("HandlerLeak") object : Handler() {
        override fun handleMessage(message: Message) {
            val i = message.what //获取消息里的数值
            val imageView = message.obj as ImageView  //获取消息里的东西，也就是创建的图片布局
            imageView.scaleType = ImageView.ScaleType.FIT_XY //设置布局图片的缩放模式
            imageView.setImageResource(R.mipmap.d) //为布局添加图片
            viewBinding.rlPoker.addView(imageView, imagewidth, imageheight)//把创建的布局添加到根布局

            val random = Random() //获取旋转的角度
            val randomX = random.nextInt(60) + 690 //计算随机数。在60度范围内随机产生，最小690最大750. 720是两圈。随机产生的度数即为两圈加减30度范围内。//每一张都有一个随机度数，所以不能写成全局变量

            //设置动画

            //设置X轴移动动画，参数依次为；控件、动画效果（translationX，沿X轴位移）、开始坐标、目的坐标（左边距+  （图片宽-重叠宽）*（  （第几张-1）  %9））。
            //			ObjectAnimator imgObjX = ObjectAnimator.ofFloat(iv,        "translationX", 0f, paddingLeft + (imgWith - overWith)*((i - 1) % 9));
            val animatorX = ObjectAnimator.ofFloat(imageView, "translationX", 0f, (padingleft + (imagewidth + overlapwidh) * ((i - 1) % mScale)).toFloat()) //设置Y轴移动动画，参数依次为；控件、动画效果（translationY，沿Y轴位移）、开始坐标、目的坐标（上边距+  （图片高-重叠高）*（  （第几张-1）*9）/81）。
            val animatorY = ObjectAnimator.ofFloat(imageView, "translationY", (height - imageheight).toFloat(), (padingtop + (imageheight - overlapheight) * ((i - 1) * mScale / POSITION)).toFloat()) //设置旋转动画 参数依次为；控件、动画效果（rotation，旋转）、角度。
            val animatorR = ObjectAnimator.ofFloat(imageView, "rotation", randomX.toFloat())


            val animatorSet = AnimatorSet()  //开始动画
            animatorSet.duration = 2000 //设置时间
            animatorSet.play(animatorX).with(animatorY).with(animatorR) //关联动画 同时
            animatorSet.start() //开启动画


            if (i == DISPLAY) {    //判断最后一张是否显示完
                viewBinding.rlPoker.setOnClickListener(View.OnClickListener {
                    viewBinding.rlPoker.setEnabled(false)  //关闭点击状态
                    object : Thread() {
                        override fun run() {
                            for (j in list.indices) {
                                val message = Message() //给消息添加东西
                                message.obj = list[j]
                                message.what = j
                                handlerOver.sendMessage(message)
                            }
                        }
                    }.start()//开新线程
                    runOnUiThread { //创建图片
                        val car = TextView(this@PokerActivity)
                        car.background = getDrawable(R.mipmap.c)
                        car.gravity = Gravity.CENTER //                                                                car.setTextColor(ResUtil.getColor("#FFFFFF"));

                        //计算图片宽高
                        val carw = width / 10 * 6
                        val carh = height / 10 * 6
                        viewBinding.rlPoker.addView(car, carw, carh)//添加到布局里
                        val animatorXX = ObjectAnimator.ofFloat(car, "translationX", ((width - padingleft - padingright) / 4).toFloat(), ((width - padingleft - padingright) / 4).toFloat())
                        val animatorYY = ObjectAnimator.ofFloat(car, "translationY", height.toFloat(), (height / 10).toFloat())
                        val animatorSet = AnimatorSet()
                        animatorSet.duration = 2000
                        animatorSet.play(animatorXX).with(animatorYY)
                        animatorSet.start()
                        animatorSet.addListener(object : Animator.AnimatorListener {
                            override fun onAnimationStart(animation: Animator) {}
                            override fun onAnimationEnd(animation: Animator) {
                                car.setText(getKeyBookList().get(RandomUtil.getInt(getKeyBookList().size)).getAnswer())
                                viewBinding.tvPokerReturn.setVisibility(View.VISIBLE)
                            }

                            override fun onAnimationCancel(animation: Animator) {}
                            override fun onAnimationRepeat(animation: Animator) {}
                        })
                    }
                })
            }
        }
    }

    /**
     * 图片下落动画
     */
    @SuppressLint("HandlerLeak") private val handlerOver: Handler = object : Handler() {
        override fun handleMessage(message: Message) { //获取消息里的数据
            val j = message.what
            val imageView = message.obj as ImageView


            val loction = IntArray(2) //获取当前图片坐标
//            imageView.getLocationInWindow(loction) // 获取图片在窗口的绝对坐标
            imageView.getLocationOnScreen(loction) //获取图片在屏幕的绝对坐标

            //获取旋转随机度数
            val random = Random()
            val randomR = random.nextInt(60) + 690


            //计算动画的移动位置
            val animatorX: ObjectAnimator = ObjectAnimator.ofFloat(imageView, "translationX", loction[0].toFloat(), (width / 2).toFloat())
            val animatorY: ObjectAnimator = ObjectAnimator.ofFloat(imageView, "translationY", loction[1].toFloat(), (height ).toFloat())
            val animatorR = ObjectAnimator.ofFloat(imageView,
                "rotation",
                0f,
                randomR.toFloat()) //            ObjectAnimator animator XX=ObjectAnimator.ofFloat(car, "translationX", (width-padingleft-padingright)/4,(width-padingleft-padingright)/4);
            //            ObjectAnimator animatorYY=ObjectAnimator.ofFloat(car, "translationY", height,height/10);
            val animatorSet = AnimatorSet()
            animatorSet.duration = 2000 //            animatorSet.play(animatorX).with(animatorY).with(animatorR).with(animatorXX).with(animatorYY);
            animatorSet.play(animatorX).with(animatorY).with(animatorR)
            animatorSet.start()
        }
    }
    override fun initData() {
    }
}

fun getKeyBookList(): List<KeyBookBean> {
    val list: MutableList<KeyBookBean> = ArrayList<KeyBookBean>()
    list.add(KeyBookBean("别太赶了."))
    list.add(KeyBookBean("别想了."))
    list.add(KeyBookBean("不."))
    list.add(KeyBookBean("不然呢？"))
    list.add(KeyBookBean("不要抱期望."))
    list.add(KeyBookBean("要求不要太高."))
    list.add(KeyBookBean("这是必然的，不要抗拒."))
    list.add(KeyBookBean("不要浪费精力."))
    list.add(KeyBookBean("不要浪费你的时间了."))
    list.add(KeyBookBean("不要做得过火了."))
    list.add(KeyBookBean("不用担心."))
    list.add(KeyBookBean("车道山前必有路."))
    list.add(KeyBookBean("答案就在你身边."))
    list.add(KeyBookBean("大胆一点."))
    list.add(KeyBookBean("大方一点."))
    list.add(KeyBookBean("等等."))
    list.add(KeyBookBean("放轻松点，慢慢来."))
    list.add(KeyBookBean("管他呢."))
    list.add(KeyBookBean("毫无疑问."))
    list.add(KeyBookBean("好运将会降临."))
    list.add(KeyBookBean("忽略了一件显而易见的事."))
    list.add(KeyBookBean("换个角度想想."))
    list.add(KeyBookBean("会付出代价."))
    list.add(KeyBookBean("会感到庆幸."))
    list.add(KeyBookBean("会很不顺利."))
    list.add(KeyBookBean("会很顺利."))
    list.add(KeyBookBean("会后悔的."))
    list.add(KeyBookBean("会失去自我."))
    list.add(KeyBookBean("会特别顺利."))
    list.add(KeyBookBean("继续前进."))
    list.add(KeyBookBean("结果可能让人惊讶."))
    list.add(KeyBookBean("绝对不."))
    list.add(KeyBookBean("肯定的."))
    list.add(KeyBookBean("没错."))
    list.add(KeyBookBean("木已成舟."))
    list.add(KeyBookBean("你必须解决一些相关的问题."))
    list.add(KeyBookBean("不会后悔的."))
    list.add(KeyBookBean("不会失望的."))
    list.add(KeyBookBean("得真正的努力一下."))
    list.add(KeyBookBean("还有别的选择."))
    list.add(KeyBookBean("你将取得成功."))
    list.add(KeyBookBean("你说了算."))
    list.add(KeyBookBean("你心里已经有答案了."))
    list.add(KeyBookBean("需要合作."))
    list.add(KeyBookBean("需要冒险."))
    list.add(KeyBookBean("要主动."))
    list.add(KeyBookBean("你在开玩笑吗."))
    list.add(KeyBookBean("你做什么都没用."))
    list.add(KeyBookBean("奇迹即将降临."))
    list.add(KeyBookBean("千万别信."))
    list.add(KeyBookBean("千万不能失败."))
    list.add(KeyBookBean("全力以赴."))
    list.add(KeyBookBean("认清现实吧."))
    list.add(KeyBookBean("三思而后行."))
    list.add(KeyBookBean("尚待时日."))
    list.add(KeyBookBean("省点力气吧."))
    list.add(KeyBookBean("时机未到."))
    list.add(KeyBookBean("听听别人怎么说."))
    list.add(KeyBookBean("听听专家的意见."))
    list.add(KeyBookBean("万一错过，就没这个机会了."))
    list.add(KeyBookBean("为什么不呢."))
    list.add(KeyBookBean("问问你的亲人."))
    list.add(KeyBookBean("问问自己为什么要这么干."))
    list.add(KeyBookBean("先做点别的."))
    list.add(KeyBookBean("现在还说不清."))
    list.add(KeyBookBean("现在你可以."))
    list.add(KeyBookBean("相信你的直觉."))
    list.add(KeyBookBean("相信你最初的想法."))
    list.add(KeyBookBean("想的美."))
    list.add(KeyBookBean("想想有没有机会."))
    list.add(KeyBookBean("休息一下就好了."))
    list.add(KeyBookBean("学会妥协."))
    list.add(KeyBookBean("眼光长远一点."))
    list.add(KeyBookBean("要变通."))
    list.add(KeyBookBean("要有点耐心."))
    list.add(KeyBookBean("要知足."))
    list.add(KeyBookBean("要抓住问题的关键."))
    list.add(KeyBookBean("一定会有好结果的."))
    list.add(KeyBookBean("一笑了之."))
    list.add(KeyBookBean("以后再说."))
    list.add(KeyBookBean("用尽一切办法去努力."))
    list.add(KeyBookBean("有."))
    list.add(KeyBookBean("仔细想想再说."))
    list.add(KeyBookBean("再等等看."))
    list.add(KeyBookBean("障碍重重."))
    list.add(KeyBookBean("这辈子都不可能."))
    list.add(KeyBookBean("这不可取."))
    list.add(KeyBookBean("这不切实际."))
    list.add(KeyBookBean("这不值得努力."))
    list.add(KeyBookBean("这会影响你的形象."))
    list.add(KeyBookBean("这件事会很麻烦."))
    list.add(KeyBookBean("这件事你说了不算."))
    list.add(KeyBookBean("这没什么意义."))
    list.add(KeyBookBean("这取决于你的行动."))
    list.add(KeyBookBean("这是肯定的."))
    list.add(KeyBookBean("没法保证."))
    list.add(KeyBookBean("无法预测."))
    list.add(KeyBookBean("这是在浪费金钱."))
    list.add(KeyBookBean("没什么好结果."))
    list.add(KeyBookBean("值得奋斗."))
    list.add(KeyBookBean("这种事情不要告诉别人."))
    list.add(KeyBookBean("只有一次机会."))
    list.add(KeyBookBean("众所周知."))
    list.add(KeyBookBean("重新想想."))
    list.add(KeyBookBean("注意细节."))
    list.add(KeyBookBean("做最坏的打算."))
    list.add(KeyBookBean("不要纠结了."))
    list.add(KeyBookBean("毫无道理."))
    list.add(KeyBookBean("结果会让你惊喜."))
    list.add(KeyBookBean("这不是你想要的."))
    list.add(KeyBookBean("寻找机会就行."))
    list.add(KeyBookBean("不要自作多情."))
    list.add(KeyBookBean("最后一次机会."))
    list.add(KeyBookBean("顺其自然."))
    list.add(KeyBookBean("学会释然."))
    list.add(KeyBookBean("别瞎折腾了."))
    list.add(KeyBookBean("再过几年."))
    list.add(KeyBookBean("不要忽视自己的力量."))
    list.add(KeyBookBean("尽在掌握之中."))
    list.add(KeyBookBean("地点不合适."))
    list.add(KeyBookBean("不作死就不会死."))
    list.add(KeyBookBean("当局者迷."))
    list.add(KeyBookBean("另择吉日."))
    list.add(KeyBookBean("时间会证明一切."))
    list.add(KeyBookBean("并非永恒."))
    list.add(KeyBookBean("倾听你内心的声音."))
    list.add(KeyBookBean("如你所愿."))
    list.add(KeyBookBean("遇上方知有."))
    list.add(KeyBookBean("不值得."))
    list.add(KeyBookBean("见好就收."))
    list.add(KeyBookBean("你猜."))
    list.add(KeyBookBean("时机尚不成熟."))
    list.add(KeyBookBean("这难以置信."))
    list.add(KeyBookBean("与你无关."))
    list.add(KeyBookBean("放手."))
    list.add(KeyBookBean("拭目以待."))
    for (i in list.indices) {
        list[i].setNumber(i + 1)
    }
    return list
}